package gw.com.android.ui.me;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import com.gwtsz.android.rxbus.RxBus;

import gw.com.android.app.AppMain;
import gw.com.android.terminal.AppTerminal;
import gw.com.jni.library.terminal.GTSConst;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import www.com.library.app.Logger;

/**
 * @author jett
 * @since 2018-03-05.
 */
public class PhoneNumber {

    private static final String TAG = "PhoneNumber";
    private static final PhoneNumber instance = new PhoneNumber();
    private static final int TIMEOUT = 10 * 1000;

    private String num;
    private final Handler mHandler;
    private Runnable failedTask;

    private PhoneNumber() {
        mHandler = AppMain.getApp().getHandler();
    }

    public static PhoneNumber getInstance() {
        return instance;
    }

    public String get() {
        return num;
    }

    public boolean exist() {
        return !TextUtils.isEmpty(num);
    }

    public void reset() {
        cancelFailedTask();
        num = null;
    }

    public void acquireAsync(final OnPhoneListener listener) {
        if (!TextUtils.isEmpty(num)) {
            if (listener != null) {
                listener.onSuccess(num);
            }
            return;
        }
        prepareFailedTask(listener);
        final CompositeDisposable disposable = new CompositeDisposable();
        Disposable subscribe = RxBus.getInstance().register(GTSConst.REPLY_MOBILE_INFO + "", Bundle.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {
                    @Override
                    public void accept(@NonNull Bundle bundle) throws Exception {
                        disposable.clear();
                        if (null != bundle && bundle.getInt("iValue") == 0) {
                            num = bundle.getString("strObject");
                            if (!TextUtils.isEmpty(num)) {
                                Logger.i(TAG, "acquire phone number succuss. num = " + num);
                                num = bundle.getString("strObject");
                                cancelFailedTask();
                                if (listener != null) {
                                    listener.onSuccess(num);
                                }
                            }
                        }
                    }
                });
        disposable.add(subscribe);
        AppTerminal.instance().getMobileNumber();
    }

    private void prepareFailedTask(final OnPhoneListener listener) {
        if (listener == null) {
            return;
        }
        failedTask = new Runnable() {
            @Override
            public void run() {
                Logger.i(TAG, "acquire phone number failed.");
                listener.onFailure();
            }
        };
        mHandler.postDelayed(failedTask, TIMEOUT);
    }

    private void cancelFailedTask() {
        if (failedTask != null) {
            mHandler.removeCallbacks(failedTask);
            failedTask = null;
        }
    }

    public static abstract class OnPhoneListener {

        public abstract void onSuccess(String num);

        public void onFailure() {
        }

    }

}
